package com.bocai.freeman.view.activitys;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseActivity;
import com.bocai.freeman.utils.ToolbarHelper;
import com.bocai.freeman.view.adapter.ViewPagerAdapter;
import com.bocai.freeman.view.fragments.SelectSkillFrag;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by admin on 2016/5/25.
 */
public class SelectSkillActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.rbt_chat)
    RadioButton mRbtChat;
    @Bind(R.id.rbt_active)
    RadioButton mRbtActive;
    @Bind(R.id.rbt_service)
    RadioButton mRbtService;
    @Bind(R.id.radioGroup)
    RadioGroup mRadioGroup;
    @Bind(R.id.viewPager)
    ViewPager mViewPager;

    private ArrayList<Fragment> mFragments;
    private ViewPagerAdapter mAdapter;
    private int[] _id = new int[]{R.id.rbt_chat,R.id.rbt_active,R.id.rbt_service};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_skill);
        ButterKnife.bind(this);
        initEvent();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ToolbarHelper.setup(this,R.string.select_skill,R.mipmap.left_return);
        mToolbar.setOnMenuItemClickListener(item -> {
            // TODO: 2016/5/25 do something
            return true;
        });

        mFragments = new ArrayList<>();
        mFragments.add(new SelectSkillFrag());
        mFragments.add(new SelectSkillFrag());
        mFragments.add(new SelectSkillFrag());
        mAdapter = new ViewPagerAdapter(getSupportFragmentManager(),mFragments);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(3);

        mRadioGroup.check(R.id.rbt_chat);
        mViewPager.setCurrentItem(0);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mRadioGroup.check(_id[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId){
                case R.id.rbt_chat:
                    mViewPager.setCurrentItem(0);
                    break;
                case R.id.rbt_active:
                    mViewPager.setCurrentItem(1);
                    break;
                case R.id.rbt_service:
                    mViewPager.setCurrentItem(2);
                    break;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_confirm,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
